#include "mainsense.h"
#include "ui_mainsense.h"
#include"config.h"
#include<QIcon>
#include<QPainter>
#include<QMouseEvent>
#include <ctime>
#include<QSound>
mainsense::mainsense(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mainsense)
{
    initscene();
    playGame();
}

mainsense::~mainsense()
{
    delete ui;
}
void mainsense:: initscene(){
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    setWindowTitle(GAME_TITLE);
    setWindowIcon(QPixmap(":/res/me1.png"));

    m_Timer.setInterval(10);

    m_recorder=0;

//随机数种子
    srand((unsigned int)time(NULL));
}

void mainsense::playGame(){
    QSound::play(":/res/background.wav");

    m_Timer.start();

    connect(&m_Timer,&QTimer::timeout,[=](){
        enemyToScene();
        updatePosition();

        update();

        collisionDetection();
    });
}
void mainsense::updatePosition(){
    m_map.mapPosition();

    m_hero.shoot();

    for(int i=0;i<30;i++){
        if(m_hero.m_bullets[i].m_Free==false){
            m_hero.m_bullets[i].updatePosition();
        }
    }

    for(int i=0;i<20;i++){
        if(m_enemys[i].m_Free==false){
            m_enemys[i].updatePosition();
        }
    }
    for(int i=0;i<20;i++){
        if(m_bombs[i].m_Free==false){
            m_bombs[i].updateInfo();
        }
    }
//    temp_bullet.m_Free =false;
//    temp_bullet.updatePosition();
}
void mainsense::paintEvent(QPaintEvent *){
    QPainter painter(this);
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_plane);

    for(int i=0;i<30;i++){
        if(m_hero.m_bullets[i].m_Free==false){
             painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_bullet);
        }

    }
    for(int i=0;i<20;i++){
        if(m_enemys[i].m_Free==false){
            painter.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
        }
    }
    for(int i=0;i<20;i++){
        if(m_bombs[i].m_Free==false){
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }
//   painter.drawPixmap(temp_bullet.m_X,temp_bullet.m_Y,temp_bullet.m_bullet);
}

void mainsense::mouseMoveEvent(QMouseEvent *event){
    int x=event->x()-m_hero.m_Rect.width()*0.5;
    int y=event->y()-m_hero.m_Rect.height()*0.5;

    if(x<=0){
        x=0;
    }
    if(x>=512-m_hero.m_Rect.width()){
        x=512-m_hero.m_Rect.width();
    }
    if(y<=0){
        y=0;
    }
    if(y>=768-m_hero.m_Rect.height()){
        y=768-m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);
}

void mainsense::enemyToScene()
{
    m_recorder++;
    if(m_recorder<30){
        return;
    }
    m_recorder=0;
    for(int i=0;i<20;i++){
        if(m_enemys[i].m_Free){
            m_enemys[i].m_Free=false;
            m_enemys[i].m_X=rand()%(512-m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y=-m_enemys[i].m_Rect.height();
            break;
        }
    }
}

void mainsense::collisionDetection()
{
    for(int i=0;i<20;i++){
        if(m_enemys[i].m_Free){
            continue;
        }
        for(int j=0;j<30;j++){
            if(m_hero.m_bullets[j].m_Free){
                continue;
            }
            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect)){
                m_enemys[i].m_Free=true;
                m_hero.m_bullets[j].m_Free=true;

                for(int k=0;k<4;k++){
                    if(m_bombs[k].m_Free){
                        QSound::play(":/res/down.wav");
                        m_bombs[k].m_Free=false;
                        m_bombs[k].m_X=m_enemys[i].m_X;
                        m_bombs[k].m_Y=m_enemys[i].m_Y;
                        break;
                    }
                }
            }
        }
    }
}
